/*
 * Copyright 2015 guohuanwen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bcgtgjyb.huanwen.customview.mylibrary;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by Administrator on 2015/11/9.
 */
public class FiveLine extends Component implements Component.DrawTask {
    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
    private Paint paint;
    private boolean isInit = false;
    private float width = 0;
    private float height = 0;
    private DataAnimatorValue valueAnimator;
    private float numb = 0;
    private boolean isStop = false;
    private float[] initLine = new float[]{0.6f, 0.3f, 0, 0.3f, 0.6f};

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrs 属性集
     */
    public FiveLine(Context context, AttrSet attrs) {
        super(context, attrs);
        addDrawTask(this);
        paint = new Paint();
        paint.setStrokeWidth(10);
        paint.setColor(new Color(Color.getIntColor("#ff0099cc")));
    }

    // 分段周期函数，做为偏移量
    private float fx(float numb) {
        if (numb <= 0 && numb > -1) {
            numb = Math.abs(numb);
        }
        if (numb <= -1) {
            numb = (float) (numb + 2d);
        }
        if (numb > 1) {
            numb = (float) (-numb + 2d);
        }
        return numb;
    }

    /**
     * 开始
     */
    public void start() {
        if (valueAnimator == null) {
            valueAnimator = getValueAnimator();
        } else {
            valueAnimator.start();
        }
        if (!isStop) {
            eventHandler.postTask(new Runnable() {
                @Override
                public void run() {
                    invalidate();
                    start();
                }
            }, 10);
        }
    }

    public void stop() {
        this.isStop = true;
    }

    private DataAnimatorValue getValueAnimator() {
        DataAnimatorValue animator = new DataAnimatorValue();
        animator.setStart(-1);
        animator.setEnd(1);
        animator.setDuration(1500);
        animator.setCurveType(Animator.CurveType.LINEAR);
        animator.start();
        return animator;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (!isInit) {
            width = getWidth();
            height = getHeight();
            isInit = true;
            start();
        }
        numb = (float) valueAnimator.getAnimatedValue();
        Point startPoint1 = new Point();
        startPoint1.modify((float) ((double)width / 2), (fx((float) ((double)numb + initLine[2])) * width));
        Point endPoint1 = new Point();
        endPoint1.modify((float) ((double)width / 2), (float) ((double)height - fx(numb) * width));
        canvas.drawLine(startPoint1, endPoint1, paint);
        Point startPoint2 = new Point();
        startPoint2.modify((float) ((double)width / 3), fx((float) ((double)numb + initLine[1])) * width);
        Point endPoint2 = new Point();
        endPoint2.modify((float) ((double)width / 3), (float) ((double)height - fx((float) ((double)numb + 0.3f)) * width));
        canvas.drawLine(startPoint2, endPoint2, paint);
        Point startPoint3 = new Point();
        startPoint3.modify((float) ((double)width / 6), fx((float) ((double)numb + initLine[0])) * width);
        Point endPoint3 = new Point();
        endPoint3.modify((float) ((double)width / 6), (float) ((double)height - fx((float) ((double)numb + 0.6f)) * width));
        canvas.drawLine(startPoint3, endPoint3, paint);
        Point startPoint4 = new Point();
        startPoint4.modify((float) ((double)width / 3 * 2), fx((float) ((double)numb + initLine[3])) * width);
        Point endPoint4 = new Point();
        endPoint4.modify((float) ((double)width / 3 * 2), (float) ((double)height - fx((float) ((double)numb + 0.3f)) * width));
        canvas.drawLine(startPoint4, endPoint4, paint);
        Point startPoint5 = new Point();
        startPoint5.modify((float) ((double)width / 6 * 5), fx((float) ((double)numb + initLine[4])) * width);
        Point endPoint5 = new Point();
        endPoint5.modify((float) ((double)width / 6 * 5), (float) ((double)height - fx((float) ((double)numb + 0.6f)) * width));
        canvas.drawLine(startPoint5, endPoint5, paint);
    }
}
